package com.zy.fee.roadpark.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.flyco.roundview.RoundTextView;
import com.zy.fee.roadpark.R;
import com.zy.fee.roadpark.model.M_Lot;
import com.zy.fee.roadpark.util.BarcodeCreater;
import com.zygk.library.app.CommonAdapter;
import com.zygk.library.app.CommonViewHolder;
import com.zygk.library.util.ColorUtil;
import com.zygk.library.util.Convert;
import com.zygk.library.util.DensityUtil;
import com.zygk.library.util.EditFilter;
import com.zygk.library.util.StringUtils;
import com.zygk.library.util.ToastUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Wikison on 2018/9/14.
 */
public class CommonDialog {
  public interface OnItemCallback {
    void onItemClick(int i);
  }

  public static Dialog showFilterDialog(Context context, OnItemCallback callback) {
    final Dialog dlg = new Dialog(context, R.style.MMTheme_DataSheet);
    dlg.getWindow()
        .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
            | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    LayoutInflater inflater =
        (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.alert_dialog_node_filter, null);
    final int cFullFillWidth = 10000;
    layout.setMinimumWidth(cFullFillWidth);
    RoundTextView tvAll = (RoundTextView) layout.findViewById(R.id.tv_all);
    tvAll.setTag(-1);
    RoundTextView tvUnregister = (RoundTextView) layout.findViewById(R.id.tv_unregister);
    tvUnregister.setTag(2);
    RoundTextView tvRegister = (RoundTextView) layout.findViewById(R.id.tv_register);
    tvRegister.setTag(1);
    RoundTextView tvFree = (RoundTextView) layout.findViewById(R.id.tv_free);
    tvFree.setTag(0);

    View.OnClickListener onClickListener = arg0 -> {
      callback.onItemClick((Integer) arg0.getTag());
      dlg.dismiss();
    };

    tvAll.setOnClickListener(onClickListener);
    tvUnregister.setOnClickListener(onClickListener);
    tvRegister.setOnClickListener(onClickListener);
    tvFree.setOnClickListener(onClickListener);

    Window w = dlg.getWindow();
    WindowManager.LayoutParams lp = w.getAttributes();
    lp.x = 0;
    final int cMakeBottom = -1000;
    lp.y = cMakeBottom;
    lp.gravity = Gravity.BOTTOM;
    dlg.onWindowAttributesChanged(lp);
    dlg.setCanceledOnTouchOutside(true);
    dlg.setContentView(layout);
    dlg.show();

    return dlg;
  }

  public static Dialog showLotListDialog(Context context, List<M_Lot> lotList,
      final OnItemCallback callback) {
    final Dialog dlg = new Dialog(context, R.style.MMTheme_DataSheet);
    dlg.getWindow()
        .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
            | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    LayoutInflater inflater =
        (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.alert_dialog_lot_list, null);
    final int cFullFillWidth = 10000;
    layout.setMinimumWidth(cFullFillWidth);
    ListView listView = (ListView) layout.findViewById(R.id.listView);
    listView.setAdapter(new CommonAdapter<M_Lot>(context, R.layout.item_dialog_lot, lotList) {
      @Override public void convert(CommonViewHolder holder, M_Lot mLot, final int position) {
        holder.setText(R.id.tv_name, mLot.getName());
      }
    });
    listView.setOnItemClickListener((parent, view, position, id) -> {
      callback.onItemClick(position);
      dlg.dismiss();
    });

    dlg.setOnKeyListener((dialog, keyCode, event) -> {
      if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        return true;
      } else {
        return false;
      }
    });
    dlg.setCanceledOnTouchOutside(false);
    dlg.setContentView(layout);
    dlg.show();

    return dlg;
  }

  public static Dialog showQrCodeDialog(Context context, String qrUrl) {
    final Dialog dlg = new Dialog(context, R.style.MMTheme_DataSheet);
    dlg.getWindow()
        .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
            | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    LayoutInflater inflater =
        (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout layout =
        (LinearLayout) inflater.inflate(R.layout.alert_dialog_qrcode, null);
    TextView tvClose = (TextView) layout.findViewById(R.id.tv_close);
    ImageView ivQr = (ImageView) layout.findViewById(R.id.iv_qr);
    String url = qrUrl;
    Bitmap mBitmap = BarcodeCreater.encode2dAsBitmap(url, DensityUtil.dp2px(220), DensityUtil.dp2px(220), 2);
    ivQr.setImageBitmap(mBitmap);
    final int cFullFillWidth = 10000;
    layout.setMinimumWidth(cFullFillWidth);

    tvClose.setOnClickListener(v -> {
      dlg.dismiss();
    });

    dlg.setCanceledOnTouchOutside(true);
    dlg.setContentView(layout);
    dlg.show();

    return dlg;
  }

  public static Dialog showQrCodeDialog(Context context, String qrUrl, String plateNum,
      double payMoney) {
    final Dialog dlg = new Dialog(context, R.style.MMTheme_DataSheet);
    dlg.getWindow()
        .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
            | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    LayoutInflater inflater =
        (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout layout =
        (LinearLayout) inflater.inflate(R.layout.alert_dialog_qrcode_online, null);
    TextView tvClose = (TextView) layout.findViewById(R.id.tv_close);
    ImageView ivQr = (ImageView) layout.findViewById(R.id.iv_qr);
    TextView tvPlateNum = (TextView) layout.findViewById(R.id.tv_plate_number);
    TextView tvPayMoney = (TextView) layout.findViewById(R.id.tv_money);
    tvPlateNum.setText(plateNum);
    tvPayMoney.setText(Convert.getMoneyString(payMoney) + "元");
    String url = qrUrl;
    Bitmap mBitmap = BarcodeCreater.encode2dAsBitmap(url, DensityUtil.dp2px(220), DensityUtil.dp2px(220), 2);
    ivQr.setImageBitmap(mBitmap);
    final int cFullFillWidth = 10000;
    layout.setMinimumWidth(cFullFillWidth);

    tvClose.setOnClickListener(v -> {
      dlg.dismiss();
    });

    dlg.setCanceledOnTouchOutside(true);
    dlg.setContentView(layout);
    dlg.show();

    return dlg;
  }

  public static Dialog showYesDialog(Context mContext, String title, String msg, String operate,
      final OnYesCallback callback) {
    final Dialog dlg = new Dialog(mContext, R.style.MMTheme_DataSheet);
    dlg.getWindow()
        .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
            | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    LayoutInflater inflater =
        (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.alert_dialog_yes_layout, null);
    final int cFullFillWidth = 10000;
    layout.setMinimumWidth(cFullFillWidth);
    TextView tvTitle = (TextView) layout.findViewById(R.id.tv_title);
    TextView tvMsg = (TextView) layout.findViewById(R.id.tv_msg);
    TextView tvYes = (TextView) layout.findViewById(R.id.tv_yes);
    tvYes.getPaint().setFakeBoldText(true);

    if (!StringUtils.isBlank(title)) {
      tvTitle.setVisibility(View.VISIBLE);
      tvTitle.setText(title);
    } else {
      tvTitle.setVisibility(View.GONE);
    }
    if (!StringUtils.isBlank(msg)) tvMsg.setText(msg);
    if (!StringUtils.isBlank(operate)) tvYes.setText(operate);

    tvYes.setOnClickListener(new View.OnClickListener() {

      @Override public void onClick(View arg0) {
        dlg.dismiss();
        if (callback != null) callback.onYesClick();
      }
    });
    dlg.setCanceledOnTouchOutside(false);
    dlg.setContentView(layout);
    dlg.show();

    return dlg;
  }

  public interface OnYesCallback {
    void onYesClick();
  }

  public interface OnNoCallback {
    void onNoClick();
  }

  public interface OnAddCallback {
    void onAddClick();
  }

  public static Dialog showYesOrNoDialog(Context context, String msg, String leftTxt,
      String rightTxt, final OnYesCallback yesCallback, final OnNoCallback noCallback) {
    final Dialog dlg = new Dialog(context, R.style.MMTheme_DataSheet);
    dlg.getWindow()
        .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
            | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    LayoutInflater inflater =
        (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout layout =
        (LinearLayout) inflater.inflate(R.layout.alert_dialog_yes_no_layout, null);
    final int cFullFillWidth = 10000;
    layout.setMinimumWidth(cFullFillWidth);
    TextView tvMsg = (TextView) layout.findViewById(R.id.tv_msg);
    TextView tvYes = (TextView) layout.findViewById(R.id.tv_yes);
    TextView tvNo = (TextView) layout.findViewById(R.id.tv_no);

    if (!StringUtils.isBlank(msg)) tvMsg.setText(msg);
    if (!StringUtils.isBlank(leftTxt)) tvNo.setText(leftTxt);
    if (!StringUtils.isBlank(rightTxt)) tvYes.setText(rightTxt);

    tvYes.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View arg0) {
        dlg.dismiss();
        if (yesCallback != null) yesCallback.onYesClick();
      }
    });
    tvNo.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View arg0) {
        dlg.dismiss();
        if (noCallback != null) noCallback.onNoClick();
      }
    });
    dlg.setCanceledOnTouchOutside(false);
    dlg.setContentView(layout);
    dlg.show();

    return dlg;
  }

  public static Dialog showYesOrNoWithTitleDialog(Context context, String title, String msg,
      String leftTxt, String rightTxt, final OnYesCallback yesCallback,
      final OnNoCallback noCallback) {
    final Dialog dlg = new Dialog(context, R.style.MMTheme_DataSheet);
    dlg.getWindow()
        .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
            | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    LayoutInflater inflater =
        (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout layout =
        (LinearLayout) inflater.inflate(R.layout.alert_dialog_yes_no_layout, null);
    final int cFullFillWidth = 10000;
    layout.setMinimumWidth(cFullFillWidth);
    TextView tvTitle = (TextView) layout.findViewById(R.id.tv_title);
    TextView tvMsg = (TextView) layout.findViewById(R.id.tv_msg);
    TextView tvYes = (TextView) layout.findViewById(R.id.tv_yes);
    TextView tvNo = (TextView) layout.findViewById(R.id.tv_no);

    if (!StringUtils.isBlank(title)) {
      tvTitle.setVisibility(View.VISIBLE);
      tvTitle.setText(title);
    }
    if (!StringUtils.isBlank(msg)) tvMsg.setText(msg);
    if (!StringUtils.isBlank(leftTxt)) tvNo.setText(leftTxt);
    if (!StringUtils.isBlank(rightTxt)) tvYes.setText(rightTxt);

    tvYes.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View arg0) {
        dlg.dismiss();
        if (yesCallback != null) yesCallback.onYesClick();
      }
    });
    tvNo.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View arg0) {
        dlg.dismiss();
        if (noCallback != null) noCallback.onNoClick();
      }
    });
    dlg.setCanceledOnTouchOutside(false);
    dlg.setContentView(layout);
    dlg.show();

    return dlg;
  }

  public interface OnChooseCallback {
    void onChooseCallback(int index);
  }

  public static void showPickerView(final Context mContext, final ArrayList<String> dataList,
      final TextView textView, String pickViewText, final OnChooseCallback onChooseCallback) {
    OptionsPickerView pvOptions = new OptionsPickerBuilder(mContext, new OnOptionsSelectListener() {
      @Override public void onOptionsSelect(int options1, int options2, int options3, View v) {
        if (textView != null) {
          textView.setText(dataList.get(options1));
        }
        if (onChooseCallback != null) onChooseCallback.onChooseCallback(options1);
      }
    }).setTitleText(pickViewText)
        .setDividerColor(Color.BLACK)
        .setCancelColor(ColorUtil.getColor(mContext, R.color.font_black_333))
        .setSubmitColor(ColorUtil.getColor(mContext, R.color.theme_color))
        .setTextColorCenter(Color.BLACK)
        .setContentTextSize(20)
        .setOutSideCancelable(false)
        .build();

    pvOptions.setPicker(dataList);
    pvOptions.show();
  }

  public static Dialog showConfirmPayDialog(Context context, String carNumber, double money,
      String leftTxt, String rightTxt, final OnYesCallback yesCallback,
      final OnNoCallback noCallback) {
    final Dialog dlg = new Dialog(context, R.style.MMTheme_DataSheet);
    dlg.getWindow()
        .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
            | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    LayoutInflater inflater =
        (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.alert_dialog_confirm_pay, null);
    final int cFullFillWidth = 10000;
    layout.setMinimumWidth(cFullFillWidth);
    TextView tvPlateNumber = (TextView) layout.findViewById(R.id.tv_plate_number);
    TextView tvMoney = (TextView) layout.findViewById(R.id.tv_money);
    TextView tvYes = (TextView) layout.findViewById(R.id.tv_yes);
    TextView tvNo = (TextView) layout.findViewById(R.id.tv_no);

    tvPlateNumber.setText(carNumber);
    tvMoney.setText(Convert.getMoneyString(money) + "");
    if (!StringUtils.isBlank(leftTxt)) tvNo.setText(leftTxt);
    if (!StringUtils.isBlank(rightTxt)) tvYes.setText(rightTxt);

    tvYes.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View arg0) {
        dlg.dismiss();
        if (yesCallback != null) yesCallback.onYesClick();
      }
    });
    tvNo.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View arg0) {
        dlg.dismiss();
        if (noCallback != null) noCallback.onNoClick();
      }
    });
    dlg.setCanceledOnTouchOutside(false);
    dlg.setContentView(layout);
    dlg.show();

    return dlg;
  }

  public interface OnInputCallback {
    void onInput(String input);
  }

  /**
   * 输入预收金额对话框
   *
   * @param mContext 上下文
   * @param noCallback 回调
   * @param inputCallback 回调
   */
  public static Dialog showPayedMoneyDialog(final Context mContext, String title, boolean mustInput,
      final OnNoCallback noCallback, final OnInputCallback inputCallback) {
    final Dialog dlg = new Dialog(mContext, R.style.MMTheme_DataSheet);
    dlg.getWindow()
        .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
            | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    LayoutInflater inflater =
        (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout layout =
        (LinearLayout) inflater.inflate(R.layout.alert_dialog_input_pay_money, null);
    final int cFullFillWidth = 10000;
    layout.setMinimumWidth(cFullFillWidth);

    final EditText et = (EditText) layout.findViewById(R.id.et);
    et.setFocusable(true);
    et.setFocusableInTouchMode(true);
    et.requestFocus();
    EditFilter.CashFilter(et, 1000);
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
      public void run() {
        InputMethodManager inputManager =
            (InputMethodManager) et.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(et, 0);
      }
    }, 500);
    TextView tvYes = (TextView) layout.findViewById(R.id.tv_yes);
    TextView tvNo = (TextView) layout.findViewById(R.id.tv_no);
    TextView tvTitle = (TextView) layout.findViewById(R.id.tv_title);
    if (!StringUtils.isBlank(title)) {
      tvTitle.setText(title);
    }

    tvYes.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View arg0) {
        if (mustInput && StringUtils.isBlank(et.getText().toString())) {
          ToastUtil.showMessage(mContext, "请输入预收金额");
          return;
        }
        dlg.dismiss();
        if (inputCallback != null) inputCallback.onInput(et.getText().toString());
      }
    });
    tvNo.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View arg0) {
        dlg.dismiss();
        if (noCallback != null) noCallback.onNoClick();
      }
    });
    dlg.setCanceledOnTouchOutside(false);
    dlg.setContentView(layout);
    dlg.show();

    return dlg;
  }

  public static Dialog showStopOrderDialog(Context context, String msg,
      final OnYesCallback yesCallback, final OnAddCallback addCallback,
      final OnNoCallback noCallback) {
    final Dialog dlg = new Dialog(context, R.style.MMTheme_DataSheet);
    dlg.getWindow()
        .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
            | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    LayoutInflater inflater =
        (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout layout =
        (LinearLayout) inflater.inflate(R.layout.alert_dialog_stop_order_layout, null);
    final int cFullFillWidth = 10000;
    layout.setMinimumWidth(cFullFillWidth);
    TextView tvMsg = (TextView) layout.findViewById(R.id.tv_msg);
    TextView tvYes = (TextView) layout.findViewById(R.id.tv_yes);
    TextView tvAdd = (TextView) layout.findViewById(R.id.tv_add);
    RoundTextView tvNo = (RoundTextView) layout.findViewById(R.id.tv_no);

    if (!StringUtils.isBlank(msg)) tvMsg.setText(msg);

    tvYes.setOnClickListener(arg0 -> {
      dlg.dismiss();
      if (yesCallback != null) yesCallback.onYesClick();
    });

    tvAdd.setOnClickListener(v -> {
      dlg.dismiss();
      if (addCallback != null) addCallback.onAddClick();
    });

    tvNo.setOnClickListener(arg0 -> {
      dlg.dismiss();
      if (noCallback != null) noCallback.onNoClick();
    });

    dlg.setCanceledOnTouchOutside(false);
    dlg.setContentView(layout);
    dlg.show();

    return dlg;
  }

  /**
   * 选择照片
   */
  public interface AddPhotoCallback {
    void onTakePic();

    void onChoosePoc();
  }

  public static Dialog addPhotoDialog(Context context, final AddPhotoCallback callback) {
    final Dialog dlg = new Dialog(context, R.style.MMTheme_DataSheet);
    dlg.getWindow()
        .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
            | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    LayoutInflater inflater =
        (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout layout =
        (LinearLayout) inflater.inflate(R.layout.alert_dialog_addphoto_layout, null);
    final int cFullFillWidth = 10000;
    layout.setMinimumWidth(cFullFillWidth);
    TextView tvTakePhoto = (TextView) layout.findViewById(R.id.tv_take_photo);
    TextView tvGetPhoto = (TextView) layout.findViewById(R.id.tv_get_photo);
    TextView tvCancle = (TextView) layout.findViewById(R.id.tv_cancle);
    tvGetPhoto.setOnClickListener(new View.OnClickListener() {

      @Override public void onClick(View arg0) {
        callback.onChoosePoc();
        dlg.dismiss();
      }
    });
    tvTakePhoto.setOnClickListener(new View.OnClickListener() {

      @Override public void onClick(View arg0) {
        callback.onTakePic();
        dlg.dismiss();
      }
    });
    tvCancle.setOnClickListener(new View.OnClickListener() {

      @Override public void onClick(View arg0) {
        dlg.dismiss();
      }
    });
    // set a large value put it in bottom
    Window w = dlg.getWindow();
    WindowManager.LayoutParams lp = w.getAttributes();
    lp.x = 0;
    final int cMakeBottom = -1000;
    lp.y = cMakeBottom;
    lp.gravity = Gravity.BOTTOM;
    dlg.onWindowAttributesChanged(lp);
    dlg.setCanceledOnTouchOutside(true);
    dlg.setContentView(layout);
    dlg.show();

    return dlg;
  }
}
